package com.huawei.hms.framework.network.restclient.hwhttp.netdiag;

import android.content.Context;
import android.os.SystemClock;
import com.huawei.hms.framework.common.IoUtils;
import com.huawei.hms.framework.common.Logger;
import com.huawei.hms.framework.network.restclient.R;
import com.huawei.hms.framework.network.util.ContextUtil;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.nio.charset.Charset;

public class UdpDnsQuery implements IQuery {
    private static final int ADDITIONAL_RRS = 0;
    private static final int ANSWER_RRS = 0;
    private static final int AUTHORITY_RRS = 0;
    private static final int BUF_SIZE = 1024;
    private static final int DNS_PORT = 53;
    private static final int FLAGS = 256;
    private static final int POINTER = 192;
    private static final int QUERY_CLASS = 1;
    private static final int QUERY_TYPE = 1;
    private static final int QUESTIONS = 1;
    private static final String TAG = "UdpDnsQuery";
    private static final int TIME_OUT = 3000;
    private static final int TRANSACTION_ID = 1;
    private static final int UNIT_FILED_SIZE = 2;

    public QueryMetrics query(String str) {
        DatagramSocket datagramSocket;
        Throwable th;
        long elapsedRealtime;
        Throwable th2;
        Context context = ContextUtil.getContext();
        QueryMetrics queryMetrics = new QueryMetrics();
        DatagramSocket datagramSocket2 = null;
        OutputStream byteArrayOutputStream = new ByteArrayOutputStream(1024);
        OutputStream dataOutputStream = new DataOutputStream(byteArrayOutputStream);
        byte[] bArr = new byte[1024];
        InputStream dataInputStream = new DataInputStream(new ByteArrayInputStream(bArr));
        long elapsedRealtime2 = SystemClock.elapsedRealtime();
        try {
            datagramSocket = new DatagramSocket();
            try {
                datagramSocket.setSoTimeout(TIME_OUT);
                try {
                    encodeDNSMessage(dataOutputStream, new URL(context.getString(R.string.netdiag_domain)).getHost());
                    datagramSocket.send(new DatagramPacket(byteArrayOutputStream.toByteArray(), byteArrayOutputStream.size(), InetAddress.getByName(str), 53));
                    datagramSocket.receive(new DatagramPacket(bArr, bArr.length));
                    if (decodeDNSMessage(dataInputStream)) {
                        queryMetrics.setStatusCode(0);
                    } else {
                        queryMetrics.setStatusCode(3);
                    }
                    elapsedRealtime2 = SystemClock.elapsedRealtime() - elapsedRealtime2;
                    queryMetrics.setTotalTime(elapsedRealtime2);
                    Logger.v(TAG, "  Status: " + queryMetrics.getStatusCode() + "   TotalTime: " + elapsedRealtime2);
                    if (datagramSocket != null) {
                        datagramSocket.close();
                    }
                    IoUtils.closeSecure(dataOutputStream);
                    IoUtils.closeSecure(dataInputStream);
                } catch (Throwable th3) {
                    Logger.v(TAG, "DEFAULT_DOMAIN_NAME failed: " + th3);
                    elapsedRealtime2 = SystemClock.elapsedRealtime() - elapsedRealtime2;
                    queryMetrics.setTotalTime(elapsedRealtime2);
                    Logger.v(TAG, "  Status: " + queryMetrics.getStatusCode() + "   TotalTime: " + elapsedRealtime2);
                    if (datagramSocket != null) {
                        datagramSocket.close();
                    }
                    IoUtils.closeSecure(dataOutputStream);
                    IoUtils.closeSecure(dataInputStream);
                }
            } catch (SocketTimeoutException e) {
                datagramSocket2 = datagramSocket;
                try {
                    Logger.v(TAG, " UdpDnsQuery timeout");
                    queryMetrics.setStatusCode(2);
                    elapsedRealtime = SystemClock.elapsedRealtime() - elapsedRealtime2;
                    queryMetrics.setTotalTime(elapsedRealtime);
                    Logger.v(TAG, "  Status: " + queryMetrics.getStatusCode() + "   TotalTime: " + elapsedRealtime);
                    if (datagramSocket2 != null) {
                        datagramSocket2.close();
                    }
                    IoUtils.closeSecure(dataOutputStream);
                    IoUtils.closeSecure(dataInputStream);
                    return queryMetrics;
                } catch (Throwable th4) {
                    th2 = th4;
                    datagramSocket = datagramSocket2;
                    th3 = th2;
                    elapsedRealtime2 = SystemClock.elapsedRealtime() - elapsedRealtime2;
                    queryMetrics.setTotalTime(elapsedRealtime2);
                    Logger.v(TAG, "  Status: " + queryMetrics.getStatusCode() + "   TotalTime: " + elapsedRealtime2);
                    if (datagramSocket != null) {
                        datagramSocket.close();
                    }
                    IoUtils.closeSecure(dataOutputStream);
                    IoUtils.closeSecure(dataInputStream);
                    throw th3;
                }
            } catch (IOException e2) {
                try {
                    Logger.v(TAG, " UdpDnsQuery fail");
                    queryMetrics.setStatusCode(1);
                    elapsedRealtime2 = SystemClock.elapsedRealtime() - elapsedRealtime2;
                    queryMetrics.setTotalTime(elapsedRealtime2);
                    Logger.v(TAG, "  Status: " + queryMetrics.getStatusCode() + "   TotalTime: " + elapsedRealtime2);
                    if (datagramSocket != null) {
                        datagramSocket.close();
                    }
                    IoUtils.closeSecure(dataOutputStream);
                    IoUtils.closeSecure(dataInputStream);
                    return queryMetrics;
                } catch (Throwable th5) {
                    th3 = th5;
                    elapsedRealtime2 = SystemClock.elapsedRealtime() - elapsedRealtime2;
                    queryMetrics.setTotalTime(elapsedRealtime2);
                    Logger.v(TAG, "  Status: " + queryMetrics.getStatusCode() + "   TotalTime: " + elapsedRealtime2);
                    if (datagramSocket != null) {
                        datagramSocket.close();
                    }
                    IoUtils.closeSecure(dataOutputStream);
                    IoUtils.closeSecure(dataInputStream);
                    throw th3;
                }
            }
        } catch (SocketTimeoutException e3) {
            Logger.v(TAG, " UdpDnsQuery timeout");
            queryMetrics.setStatusCode(2);
            elapsedRealtime = SystemClock.elapsedRealtime() - elapsedRealtime2;
            queryMetrics.setTotalTime(elapsedRealtime);
            Logger.v(TAG, "  Status: " + queryMetrics.getStatusCode() + "   TotalTime: " + elapsedRealtime);
            if (datagramSocket2 != null) {
                datagramSocket2.close();
            }
            IoUtils.closeSecure(dataOutputStream);
            IoUtils.closeSecure(dataInputStream);
            return queryMetrics;
        } catch (IOException e4) {
            datagramSocket = null;
            Logger.v(TAG, " UdpDnsQuery fail");
            queryMetrics.setStatusCode(1);
            elapsedRealtime2 = SystemClock.elapsedRealtime() - elapsedRealtime2;
            queryMetrics.setTotalTime(elapsedRealtime2);
            Logger.v(TAG, "  Status: " + queryMetrics.getStatusCode() + "   TotalTime: " + elapsedRealtime2);
            if (datagramSocket != null) {
                datagramSocket.close();
            }
            IoUtils.closeSecure(dataOutputStream);
            IoUtils.closeSecure(dataInputStream);
            return queryMetrics;
        } catch (Throwable th42) {
            th2 = th42;
            datagramSocket = null;
            th3 = th2;
            elapsedRealtime2 = SystemClock.elapsedRealtime() - elapsedRealtime2;
            queryMetrics.setTotalTime(elapsedRealtime2);
            Logger.v(TAG, "  Status: " + queryMetrics.getStatusCode() + "   TotalTime: " + elapsedRealtime2);
            if (datagramSocket != null) {
                datagramSocket.close();
            }
            IoUtils.closeSecure(dataOutputStream);
            IoUtils.closeSecure(dataInputStream);
            throw th3;
        }
        return queryMetrics;
    }

    private void encodeDNSMessage(DataOutputStream dataOutputStream, String str) throws IOException {
        dataOutputStream.writeShort(1);
        dataOutputStream.writeShort(FLAGS);
        dataOutputStream.writeShort(1);
        dataOutputStream.writeShort(0);
        dataOutputStream.writeShort(0);
        dataOutputStream.writeShort(0);
        encodeDomainName(dataOutputStream, str);
        dataOutputStream.writeShort(1);
        dataOutputStream.writeShort(1);
        dataOutputStream.flush();
    }

    private void encodeDomainName(DataOutputStream dataOutputStream, String str) throws IOException {
        for (String str2 : str.split("\\.")) {
            dataOutputStream.writeByte((byte) str2.length());
            dataOutputStream.write(str2.getBytes(Charset.forName("UTF-8")));
        }
        dataOutputStream.writeByte(0);
    }

    private boolean decodeDNSMessage(DataInputStream dataInputStream) throws IOException {
        dataInputStream.skip(2);
        dataInputStream.skip(2);
        dataInputStream.skip(2);
        short readShort = dataInputStream.readShort();
        dataInputStream.skip(2);
        dataInputStream.skip(2);
        skipDomainName(dataInputStream);
        dataInputStream.skip(2);
        dataInputStream.skip(2);
        int[] iArr = new int[readShort];
        short s = (short) 0;
        while (s < readShort) {
            dataInputStream.mark(1);
            byte readByte = dataInputStream.readByte();
            dataInputStream.reset();
            if ((readByte & POINTER) == POINTER) {
                dataInputStream.skip(2);
            } else {
                skipDomainName(dataInputStream);
            }
            short readShort2 = dataInputStream.readShort();
            dataInputStream.skip(2);
            dataInputStream.readInt();
            short readShort3 = dataInputStream.readShort();
            if (readShort2 == (short) 1 && readShort3 == (short) 4) {
                iArr[s] = dataInputStream.readInt();
                s++;
            } else {
                dataInputStream.skip((long) readShort3);
                return false;
            }
        }
        return true;
    }

    private void skipDomainName(DataInputStream dataInputStream) throws IOException {
        byte readByte;
        do {
            readByte = dataInputStream.readByte();
            Logger.v(TAG, "labelLength" + readByte);
            dataInputStream.skip((long) readByte);
        } while (readByte != (byte) 0);
    }
}
